package emd24.telstudyresources;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class StudyModeActivity extends Activity {
	private Quiz bank;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_study_mode);;
		Bundle b = this.getIntent().getExtras();
		this.bank = b.getParcelable("bank");
		setTitle("Browse Questions - " + this.bank.getName());
		ListView v = (ListView) this.findViewById(R.id.ListView01);
		v.setAdapter(new QuestionAdapter(this,bank.toArray()));
		v.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long id) {
				openQuestion(pos);
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_study_mode, menu);
		return true;
	}
	/**
	 * Opens us a selected question 
	 * 
	 */
	private void openQuestion(int pos){
		Intent i = new Intent(this, QuestionActivity.class);
		i.putExtra("study", true);
		i.putExtra("quiz", this.bank);
		i.putExtra("qIndex", pos);
		startActivity(i);
	}
	/**
	 * This is an adapter for the ListView for StudyMode. It populates
	 * data with questions
	 * 
	 * @author Evan Dyke
	 *
	 */
	private class QuestionAdapter extends BaseAdapter {

		private Question[] questions;
		private QuestionAdapter(Context c, Question[] questions){
			this.questions = questions;
			LayoutInflater.from(c);
		}

		@Override
		public int getCount() {
			return this.questions.length;
		}

		@Override
		public Object getItem(int position) {
			return this.questions[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView qNumber;
			if(convertView == null){
				LayoutInflater inflater = LayoutInflater.from(parent.getContext());
				convertView = inflater.inflate(R.layout.studymode_row_view, parent, false);

			} 
			qNumber = (TextView) convertView.findViewById(R.id.qNumber);	
			qNumber.setText("Question " + this.questions[position].getQuestionNumber());
			return convertView;
		}

	}
}